package com.zx.map.service.impl;

import com.zx.map.dao.MerchantOfflineConfigInfoDao;
import com.zx.map.dao.model.MerchantOfflineConfigInfo;
import com.zx.map.dao.model.MerchantOfflineConfigInfoExample;
import com.zx.map.service.MerchantOfflineConfigInfoService;
import com.zx.map.service.model.MerchantOfflineConfigInfoModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 14-12-10 Time: 涓嬪崍9:10 To
 * change this template use File | Settings | File Templates.
 */
@Component("merchantOfflineConfigInfoService")
public class MerchantOfflineConfigInfoServiceImpl implements MerchantOfflineConfigInfoService {

    @Autowired
    private MerchantOfflineConfigInfoDao dao;

    /**
     * 商户号 = key 根据单Key查找Model
     */
    public MerchantOfflineConfigInfoModel getModelByKey(String key)
            throws DataAccessException {
        Assert.isTrue(StringUtils.isNotBlank(key));
        MerchantOfflineConfigInfo o = dao.selectByPrimaryKey(key);
        MerchantOfflineConfigInfoModel model = new MerchantOfflineConfigInfoModel();
        if (o != null) {
            BeanUtils.copyProperties(o, model);
        }
        return model;
    }

    /**
     * 根据Model查找列表
     */
    public List<MerchantOfflineConfigInfoModel> getListByModel(MerchantOfflineConfigInfoModel model)
            throws DataAccessException {
        MerchantOfflineConfigInfoExample example = new MerchantOfflineConfigInfoExample();
        MerchantOfflineConfigInfoExample.Criteria criteria = example
                .createCriteria();
        if (StringUtils.isNotBlank(model.getMerchantId())) {
            criteria.andMerchantIdEqualTo(model.getMerchantId());
        }
        if (StringUtils.isNotBlank(model.getMerchantName())) {
            criteria.andMerchantNameLike('%' + model.getMerchantName() + '%');
        }
        if (StringUtils.isNotBlank(model.getMerchantAddr())) {
            criteria.andMerchantAddrLike('%' + model.getMerchantAddr() + '%');
        }
        if (StringUtils.isNotBlank(model.getPassword())) {
            criteria.andPasswordEqualTo(model.getPassword());
        }
        if (StringUtils.isNotBlank(model.getOfflinePayId())) {
            criteria.andOfflinePayIdEqualTo(model.getOfflinePayId());
        }
        List<MerchantOfflineConfigInfo> list = dao.getListByExample(example);
        List<MerchantOfflineConfigInfoModel> modelList = new ArrayList<MerchantOfflineConfigInfoModel>();
        for (MerchantOfflineConfigInfo o : list) {
            MerchantOfflineConfigInfoModel temp = new MerchantOfflineConfigInfoModel();
            BeanUtils.copyProperties(o, temp);
            modelList.add(temp);
        }
        return modelList;
    }

    /**
     * 新增 base_acct_info 在新增商户 新增代理的时候要记录一条
     */
    public String add(MerchantOfflineConfigInfoModel model) throws DataAccessException,
            UnsupportedEncodingException {
        MerchantOfflineConfigInfo temp = new MerchantOfflineConfigInfo();
        BeanUtils.copyProperties(model, temp);
        String key = dao.add(temp);
        return key;
    }

    /**
     * 修改
     */
    public String update(MerchantOfflineConfigInfoModel model) throws DataAccessException {
        Assert.isTrue(StringUtils.isNotBlank(model.getMerchantId()));
        MerchantOfflineConfigInfo temp = new MerchantOfflineConfigInfo();
        BeanUtils.copyProperties(model, temp);
        dao.update(temp);
        return model.getMerchantId();
    }

    /**
     * 删除
     */
    public void delete(MerchantOfflineConfigInfoModel model) throws DataAccessException {
        Assert.isTrue(StringUtils.isNotBlank(model.getMerchantId()));
        MerchantOfflineConfigInfo temp = new MerchantOfflineConfigInfo();
        BeanUtils.copyProperties(model, temp);
        dao.delete(temp);
    }
}
